<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\common\controller;

use think\App;
use think\Controller;
use app\common\model\Auth;
use think\Db;

/**
 * api 公共控制器
 * @package app\common\controller
 */
class ApiController extends Controller
{
    /**
     * @package think\model
     * @var
     */
    protected $model;

    /**
     *用户信息
     */
    protected $member;

    /**
     * 每页显示的条数
     * @var int
     */

    public function __construct(App $app = null)
    {
        parent::__construct($app);

        $ad_version = input('android');
        if(!empty($ad_version))file_put_contents('android_version.log',"$ad_version\r\n\r\n",FILE_APPEND);

        //用户信息
        $this->member = '';
        if(empty($this->member) && !empty(input('userId')) && !empty(input('token'))){
            $_userid = input('userId');
            $_token = input('token');

            $token = Auth::token_validate('user_token', $_userid, $_token);
            $token['0'] = 1;
            if ($token['0'] == 1){
                $this->member =  Db::name('admin_user')
                    ->alias('u')
                    ->field('u.*,u.id as uid,g.name as group_name,g.app_page,g.give_sort,m.name as mill_name,m.group_ids')
                    ->leftJoin('admin_user_group g','g.id=u.group')
                    ->leftJoin('admin_user_mill m','m.id=u.mill_id')
                    ->where(['u.id'=>$_userid])
                    ->find();
                $_staff_id = input('staff_id');
                if(!empty($_staff_id)){
                    $this->member['nickname'] = Db::name('mill_staff')->where(['id'=>$_staff_id])->value('staff');
                    $this->member['uid'] = $_staff_id;
                }
            }
            else errors('该账号已在其他设备登录，请重新登录', ['uid'=>''], 4);
        }
        //小程序
        elseif(empty($this->member) && !empty(input('xcx_uid')) && !empty(input('token'))){
            $_userid = input('xcx_uid');
            $_token = input('token');

            $token = Auth::token_validate('xcx_token', $_userid, $_token);
            $token['0'] = 1;
            if ($token['0'] == 1){
                $this->member =  Db::name('admin_user')
                    ->alias('u')
                    ->field('u.*,u.id as uid,g.name as group_name')
                    ->leftJoin('admin_user_group g','g.id=u.group')
                    ->where(['u.id'=>$_userid])
                    ->find();
            }
            else errors('请重新登录', ['uid'=>''], 4);
        }

    }

    /**
     *用户登录验证
     */
    public function is_member()
    {
        if(!$this->member){
            errors('用户未登录。',['uid'=>''],4);
        }
    }
}